﻿using System;
using System.Collections.Generic;

namespace FluentTagsGenerator
{
    class TagElement : IComparable<TagElement>
    {
        public string Name { get; private set; }
        /// <summary>
        /// Does this element support inline text?
        /// </summary>
        public bool IsText { get; private set; }
        public string Comment { get; set; }
        public List<Attribute> Attributes { get; set; }

        private List<string> _childElements = null;

        public TagElement(string name, bool isText)
        {
            Name = name;
            IsText = isText;
            Attributes = new List<Attribute>();
            _childElements = new List<string>();
        }

        public void AddChildElement(string elementName)
        {
            if (_childElements.Contains(elementName))
                return;

            _childElements.Add(elementName);
        }

        public void AddChildElements(IEnumerable<string> elementNames)
        {
            foreach (var elementName in elementNames)
            {
                AddChildElement(elementName);
            }
        }

        /// <summary>
        /// Get the child elements for this element filtered from the list 
        /// of all elements.
        /// </summary>
        /// <param name="elements"></param>
        /// <returns></returns>
        public List<TagElement> AllChildElements(TagElements elements)
        {
            _childElements.Sort();

            var childElements = new List<TagElement>();
            foreach (var elementName in _childElements)
            {
                childElements.Add(elements.Find(element => element.Name == elementName));
            }

            return childElements;
        }

        public override string ToString()
        {
            return Name;
        }

        #region IComparable<Element> Members

        public int CompareTo(TagElement other)
        {
             return Name.CompareTo(other.Name);
        }

        #endregion
    }
}
